﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Text;


using PowerCreator.LMS.Paper.Entiry;
using PowerCreator.LMS.Web.Paper;
using PowerCreator.LMS.Web.Question;
using PowerCreator.LMS.Web.Course;
using PowerCreator.LMS.ViewQuery;
using PowerCreator.LMS.Common.Script;

public partial class sysadmin_QuestionManager_ManagerPaperInfo : PowerCreator.LMS.Web.Base.BasePage
{
    //ManagerPaperInfo manager = new ManagerPaperInfo();
    //ManagerPaperQuestion objpaperquestion = new ManagerPaperQuestion();
    //ManagerPaperUserAnswer objpaperuseranswer = new ManagerPaperUserAnswer();
    //ManagerUserPaperHistoryInfo objuserpaperhistoryinfo = new ManagerUserPaperHistoryInfo();
    //ManagerProjectResource objManagerProjectResource = new ManagerProjectResource();

    ManagerPaper objManagerPaper = new ManagerPaper();
    Course objCourse = new Course();
    ManagerQuestion objManagerQuestion = new ManagerQuestion();
    ViewQuery objViewQuery = new ViewQuery();

    protected void Page_Load(object sender, EventArgs e)
    {
        //if (!GetRole(Role.ManagerPaper))
        //{
        //    Script.ShowAndHistory("您没有管理试卷的权限!", -1);
        //    return;
        //}
        if (!IsPostBack)
        {
            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, 1);
            InitPage();
        }
    }
    protected override void InitPage()
    {
        lab_TitleInfo.Text = GetLanageValueByKey("lab_ManagerPaperInfo_InfoTitle", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        lab_PaperName.Text = GetLanageValueByKey("lab_AddPaperInfo_PaperName", PowerCreator.LMS.Language.LanagetItemType.Page);
        lab_PaperState.Text = GetLanageValueByKey("lab_ManagerPaperInfo_PaperState", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }
    private string GetSearch()
    {
        StringBuilder str_search = new StringBuilder();
        //str_search.Append(" 1=1 and PaperBuildState=1  ");
        str_search.Append(" 1=1 ");

       

        if (dpdlst_PaperState.SelectedValue != "-1")
        {
            str_search.Append(" and PaperResearch_BuildState=" + dpdlst_PaperState.SelectedValue);
        }
     
        if (txtPaperName.Text.Trim() != "")
        {
            str_search.Append(" and PaperResearch_Name like '%" + txtPaperName.Text + "%'");
        }


        return str_search.ToString();
    }
    private void BinderPager(string search)
    {
        AspNetPager1.RecordCount = objManagerPaper.CountPaperResearch(search);
        AspNetPager1.PageSize = LMSPageSize;
    }

    private void BinderGV(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        dt = objManagerPaper.QueryPaperResearch(search, LMSPageSize, pageIndex);
        DataColumn dc = new DataColumn("SelectTypeNew");
        dt.Columns.Add("url");
        dt.Columns.Add(dc);
        dt.Columns.Add("Index");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            dt.Rows[i]["url"] = dt.Rows[i]["PaperResearch_ID"].ToString();

            dt.Rows[i]["Index"] = (pageIndex-1)*10+i + 1;
        }
        gvPaperList.DataSource = dt;
        gvPaperList.DataBind();
    }
    
    protected void gvPaperList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "mypreview")
        {
            string url = string.Empty;

        
            url = "ShowPaperInfo.aspx?p=" + e.CommandArgument.ToString();
      
            
            Script.NewOpenURL(url);
        }
        else if (e.CommandName.ToLower() == "myeditor")
        {
    
            string url = "EditPaperInfo.aspx?p=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else if (e.CommandName.ToLower() == "mydelete")
        {
            int paperID = int.Parse(e.CommandArgument.ToString());//获取试卷ID


            #region 删除试卷信息表和试卷试题表相关记录

            objManagerPaper.DeletePaperResearch(paperID);                               //删除试卷信息表
            string cmd = "FKPRQuestionPaperID=" + paperID.ToString();
            DataTable dt_pqinfo = new DataTable();
            dt_pqinfo = objManagerPaper.QueryPaperResearchQuestion(cmd);                       //获取试卷试题信息表相关记录
            for (int i = 0; i < dt_pqinfo.Rows.Count; i++)
            {
                int PaperQuestionID = int.Parse(dt_pqinfo.Rows[i]["FKPRQuestionQuesID"].ToString());
                objManagerPaper.DeletePaperResearchQuestion(PaperQuestionID);                      //删除试卷试题信息表记录
            }
            #endregion

            #region 删除用户试卷答案表相关记录
            DataTable dt_paperuseranswer = new DataTable();
            dt_paperuseranswer = objManagerPaper.QueryPaperResearchUserAnswer("FKPRUanswerPaperID=" + paperID.ToString());
            for (int i = 0; i < dt_paperuseranswer.Rows.Count; i++)
            {
                int PUanswerID = int.Parse(dt_paperuseranswer.Rows[i]["PRUanswerID"].ToString());
                objManagerPaper.DeletePaperResearchUserAnswer(PUanswerID);
            }
            #endregion


            string search = GetSearch();
            BinderPager(search);
            BinderGV(search, AspNetPager1.CurrentPageIndex);
        }
        else if (e.CommandName == "btn_Out")
        {
            int paperID = int.Parse(e.CommandArgument.ToString());//获取试卷ID
            VOPaper pinfo = new VOPaper();

            pinfo = objManagerPaper.LoadPaper(paperID);
            if (pinfo.PaperSelectType == 2)
            {
                if (string.IsNullOrEmpty(pinfo.PaperCelueID.ToString()))
                {
                    Script.Alert(Page, "show", GetLanageValueByKey("AddPaper_msg16", PowerCreator.LMS.Language.LanagetItemType.Page));
                    return;
                }
            }
            //WordManager objWordManager = new WordManager();
            //objWordManager.outword(this, paperID);
            objManagerPaper.GetOutWord(this, paperID);
        }
        else if (e.CommandName == "btn_Statistical")
        {
            string url = "PaperResearchStatistical.aspx?p=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
        else
        {
            string url = "SetJoinTestUser.aspx?pID=" + e.CommandArgument.ToString();
            Script.RedirectURL(url);
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderPager(search);
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }
    protected void gvPaperList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton ibtnPreview = (LinkButton)e.Row.Cells[8].FindControl("ibtnPreview");
            LinkButton btn_Out = (LinkButton)e.Row.Cells[8].FindControl("btn_Out");

            e.Row.Attributes.Add("bgColor", "#F7FAFF");
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#FAEDB8'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#F7FAFF'");


            if (e.Row.Cells[5].Text == "0")
            {
                e.Row.Cells[5].Text = GetLanageValueByKey("Course_db_d5", PowerCreator.LMS.Language.LanagetItemType.Page);
            }
            else
            {
                e.Row.Cells[5].Text = GetLanageValueByKey("Course_db_d4", PowerCreator.LMS.Language.LanagetItemType.Page);
            }

            if (e.Row.Cells[7].Text == "0")
            {
                e.Row.Cells[7].Text = GetLanageValueByKey("Msg_ManagerPaperInfo_NoBuild", PowerCreator.LMS.Language.LanagetItemType.Page);
                ibtnPreview.Enabled = false;
                btn_Out.Enabled = false;
            }
            else
            {
                e.Row.Cells[7].Text = GetLanageValueByKey("Msg_ManagerPaperInfo_Build", PowerCreator.LMS.Language.LanagetItemType.Page);
                ibtnPreview.Enabled = true;
                btn_Out.Enabled = true;
            }

        } 
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearch();
        BinderGV(search, AspNetPager1.CurrentPageIndex);
    }


}
